package angry.courses.beans;

import java.util.ArrayList;
import java.util.List;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.Remote;


import angry.courses.entities.CourseEntity;

@Stateless(mappedName="angry.courses.beans.ViewBean")
@Remote
public class ViewBean implements View {
	@EJB
	private CourseDAO courseDAO;
	@EJB
	private RelationDAO relationDAO;
	@EJB
	private StudentDAO studentDAO;
	
	public List<String> getStudentsForCourse(String name) {
		List<String> studentNames = new ArrayList<String>();
		// get course
		int cid = courseDAO.retrieve(name).getID();
		// get the list of students
		List<Integer> sids = relationDAO.getStudentIdsForCourse(cid);
		for (int sid : sids) {
			studentNames.add(studentDAO.retrieve(sid).getName());
		}
		return studentNames;
	}

	public List<String> getCoursesForStudent(String name, int semester) {
		List<String> courseNames = new ArrayList<String>();
		// get student
		int sid = studentDAO.retrieveByName(name).getID();
		// get list of courses for semester
		List<Integer> cids = relationDAO.getCourseIdsForStudent(sid);
		for (int cid : cids) {
			CourseEntity courseEntity = courseDAO.retrieve(cid);
			if (courseEntity.getSemester() == semester) {
				courseNames.add(courseEntity.getName());
			}
		}
		return courseNames;
	}
	
	
}
